//
//  UIImage+Extension.swift
//  Swift-Categary
//
//  Created by lantaiyuan on 2017/9/11.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import Foundation
import UIKit

extension UIImage {
  //MARK: 绘制一张颜色图片
    class func imageWithColor(_ color:UIColor,size:CGSize) ->UIImage {
        let rect = CGRect(x:0,y:0,width:size.width,height:size.height)
        UIGraphicsBeginImageContext(rect.size)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    //MARK: --返回一张自由拉伸的图片
    //等比压缩
    class func resizedImageName(_ name:String) -> UIImage {
       return resizedImage(UIImage.init(named: name)!, left: 0.5, top: 0.5)
    }
    //
    class func resizedImageName(_ name:String,left:CGFloat,right:CGFloat) -> UIImage {
        return resizedImage(UIImage.init(named: name)!, left: left, top: right)
    }
    //
    class func resizedImage(_ image:UIImage) -> UIImage {
        return resizedImage(image, left: 0.5, top: 0.5)
    }
    
    class func resizedImage(_ image:UIImage,left:CGFloat,top:CGFloat) -> UIImage {
        return image.stretchableImage(withLeftCapWidth: Int(image.size.width * left), topCapHeight: Int(image.size.height * top))
    }
    
    //MARK: ----对图片尺寸进行压缩
    class func imageSizeCompress(_ image:UIImage,maxSize:CGSize) -> UIImage {
        let imageNewSize = UIImage.imageSize(image.size, maxSize: maxSize)
        return imageNew(image,newSize: imageNewSize)
    }
    //MARK: 根据图片size 和给予最大范围size,进行等比例计算size
    class func imageSize(_ imageSize:CGSize,maxSize:CGSize) -> CGSize {
        var imageNewSize:CGSize = maxSize
        if imageSize.width < maxSize.width && imageSize.height < maxSize.height {
            return imageSize
        } else if imageSize.width > imageSize.height{//宽大于高
            let scale = maxSize.width / imageSize.width
            imageNewSize.width = maxSize.width
            imageNewSize.height = maxSize.height * scale
        }else {
            let scale = maxSize.height / imageSize.height
            imageNewSize.width = imageSize.width * scale
            imageNewSize.height = maxSize.height
        }
        return imageNewSize
    }
    //MARK: 重新绘制一张图片生成image对象
    class func imageNew(_ image:UIImage,newSize:CGSize) -> UIImage {
        UIGraphicsBeginImageContext(newSize)
        image.draw(in: CGRect(x:0,y:0,width:newSize.width,height:newSize.height), blendMode: .normal, alpha: 1.0)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }
}
